package com.iut.sujet2.presentation;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.FileChooser;
import javafx.stage.Modality;

public class MainFrame implements Initializable {

	public static File image;
	
	private FileChooser fc_image;
	
	//Barre de menu
	
	@FXML private Menu m_image;
	@FXML private Menu m_image_m_filtre;
	
	@FXML private MenuItem menuItem_fichier_quitter;
	@FXML private MenuItem menuItem_aide_about;
	@FXML private MenuItem menuItem_panel_click;
	@FXML private MenuItem menuItem_panel_images;
	@FXML private MenuItem menuItem_image_load;
	@FXML private MenuItem menuItem_image_reset;
	@FXML private CheckMenuItem menuItem_image_recadrer;
	@FXML private MenuItem m_image_m_filtre_netb;
	@FXML private MenuItem m_image_m_filtre_sep;
	
	@FXML private BorderPane bp_main;
	@FXML private BorderPane bp_secondpane;
	
	private BorderPane bp_click;
	private BorderPane bp_image;
	
	public static ImageFrame im;

	public static DrawFrame df;

	public static SelectRect sr;


	public void initialize(URL location, ResourceBundle resources) {
		
		Main.mf = this;
		
		initPanels();
		initMenus();

		
	}

	
		

	private void initMenus() {
		
		menuItem_panel_click.setDisable(true);
		menuItem_image_reset.setDisable(true);
		menuItem_image_recadrer.setDisable(true);
		m_image_m_filtre.setDisable(true);
		m_image.setVisible(false);
		
		menuItem_panel_click.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				bp_click.setVisible(true);
				bp_image.setVisible(false);
				
				menuItem_panel_click.setDisable(true);
				menuItem_panel_images.setDisable(false);
				
				m_image.setVisible(false);
				
			}
		});
		menuItem_panel_images.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				bp_click.setVisible(false);
				bp_image.setVisible(true);
				
				menuItem_panel_click.setDisable(false);
				menuItem_panel_images.setDisable(true);
				
				m_image.setVisible(true);
				
			}
		});
		
		menuItem_fichier_quitter.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				System.exit(0);
				
			}
		});
		
		menuItem_aide_about.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				javafx.application.Platform.runLater(new Runnable() {

					public void run() {
						Label label = new Label("\t\t Click Points et Affiche Image \t\t\n\t\t par Alexis Berger\t\t");
						label.setTextAlignment(TextAlignment.CENTER);
						MessageFrame about = new MessageFrame(label, "A propos", Modality.APPLICATION_MODAL);
						about.showAndWait();
					}
				});
			}
		});
		
		
		menuItem_image_load.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				
				fc_image = new FileChooser();
				image = fc_image.showOpenDialog(Main.ps);
				if(image != null){
					im.init(image);
					m_image_m_filtre.setDisable(false);
					menuItem_image_load.setDisable(true);
					menuItem_image_reset.setDisable(false);
					menuItem_image_recadrer.setDisable(false);

				}
				
			}
		});
		
		menuItem_image_reset.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				im.reset();
				menuItem_image_load.setDisable(false);
				menuItem_image_reset.setDisable(true);
				m_image_m_filtre.setDisable(true);
				menuItem_image_recadrer.setDisable(true);


				
			}
		});
		
		m_image_m_filtre_netb.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				im.filtreNetB();
				
			}
		});
		m_image_m_filtre_sep.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				im.filtreSepia();
				
			}
		});
		
	}




	/** Charger et configurer les panels*/
	private void initPanels() {
		try {
			bp_click = new BorderPane();
			bp_image = new BorderPane();
			
			Parent imagePane = (Parent) FXMLLoader.load(getClass().getResource("ressources/ImageFrame.fxml"));
			bp_image.getChildren().add(imagePane);
			Parent drawPane = (Parent) FXMLLoader.load(getClass().getResource("ressources/DrawFrame.fxml"));
			bp_click.getChildren().add(drawPane);
			
			bp_image.setVisible(false);
			
			bp_secondpane.getChildren().add(bp_image);
			bp_secondpane.getChildren().add(bp_click);
		
			Parent selectRect = (Parent) FXMLLoader.load(getClass().getResource("ressources/SelectRect.fxml"));
			bp_secondpane.getChildren().add(selectRect);
			
		} catch (IOException e) {
			new MessageFrame(new Label("Impossible de charger l'affichage"), "Erreur fatale", Modality.APPLICATION_MODAL).showAndWait();
			e.printStackTrace();
		}
	}





	/**
	 * @return the image
	 */
	public static File getImage() {
		return image;
	}




	/**
	 * @return the fc_image
	 */
	public FileChooser getFc_image() {
		return fc_image;
	}




	/**
	 * @return the m_image
	 */
	public Menu getM_image() {
		return m_image;
	}




	/**
	 * @return the m_image_m_filtre
	 */
	public Menu getM_image_m_filtre() {
		return m_image_m_filtre;
	}




	/**
	 * @return the menuItem_fichier_quitter
	 */
	public MenuItem getMenuItem_fichier_quitter() {
		return menuItem_fichier_quitter;
	}




	/**
	 * @return the menuItem_aide_about
	 */
	public MenuItem getMenuItem_aide_about() {
		return menuItem_aide_about;
	}




	/**
	 * @return the menuItem_panel_click
	 */
	public MenuItem getMenuItem_panel_click() {
		return menuItem_panel_click;
	}




	/**
	 * @return the menuItem_panel_images
	 */
	public MenuItem getMenuItem_panel_images() {
		return menuItem_panel_images;
	}




	/**
	 * @return the menuItem_image_load
	 */
	public MenuItem getMenuItem_image_load() {
		return menuItem_image_load;
	}




	/**
	 * @return the menuItem_image_reset
	 */
	public MenuItem getMenuItem_image_reset() {
		return menuItem_image_reset;
	}




	/**
	 * @return the m_image_m_filtre_netb
	 */
	public MenuItem getM_image_m_filtre_netb() {
		return m_image_m_filtre_netb;
	}




	/**
	 * @return the m_image_m_filtre_sep
	 */
	public MenuItem getM_image_m_filtre_sep() {
		return m_image_m_filtre_sep;
	}




	/**
	 * @return the bp_main
	 */
	public BorderPane getBp_main() {
		return bp_main;
	}




	/**
	 * @return the bp_secondpane
	 */
	public BorderPane getBp_secondpane() {
		return bp_secondpane;
	}




	/**
	 * @return the bp_click
	 */
	public BorderPane getBp_click() {
		return bp_click;
	}




	/**
	 * @return the bp_image
	 */
	public BorderPane getBp_image() {
		return bp_image;
	}




	/**
	 * @return the im
	 */
	public static ImageFrame getIm() {
		return im;
	}




	/**
	 * @param image the image to set
	 */
	public static void setImage(File image) {
		MainFrame.image = image;
	}




	/**
	 * @param fc_image the fc_image to set
	 */
	public void setFc_image(FileChooser fc_image) {
		this.fc_image = fc_image;
	}




	/**
	 * @param m_image the m_image to set
	 */
	public void setM_image(Menu m_image) {
		this.m_image = m_image;
	}




	/**
	 * @param m_image_m_filtre the m_image_m_filtre to set
	 */
	public void setM_image_m_filtre(Menu m_image_m_filtre) {
		this.m_image_m_filtre = m_image_m_filtre;
	}




	/**
	 * @param menuItem_fichier_quitter the menuItem_fichier_quitter to set
	 */
	public void setMenuItem_fichier_quitter(MenuItem menuItem_fichier_quitter) {
		this.menuItem_fichier_quitter = menuItem_fichier_quitter;
	}




	/**
	 * @param menuItem_aide_about the menuItem_aide_about to set
	 */
	public void setMenuItem_aide_about(MenuItem menuItem_aide_about) {
		this.menuItem_aide_about = menuItem_aide_about;
	}




	/**
	 * @param menuItem_panel_click the menuItem_panel_click to set
	 */
	public void setMenuItem_panel_click(MenuItem menuItem_panel_click) {
		this.menuItem_panel_click = menuItem_panel_click;
	}




	/**
	 * @param menuItem_panel_images the menuItem_panel_images to set
	 */
	public void setMenuItem_panel_images(MenuItem menuItem_panel_images) {
		this.menuItem_panel_images = menuItem_panel_images;
	}




	/**
	 * @param menuItem_image_load the menuItem_image_load to set
	 */
	public void setMenuItem_image_load(MenuItem menuItem_image_load) {
		this.menuItem_image_load = menuItem_image_load;
	}




	/**
	 * @param menuItem_image_reset the menuItem_image_reset to set
	 */
	public void setMenuItem_image_reset(MenuItem menuItem_image_reset) {
		this.menuItem_image_reset = menuItem_image_reset;
	}




	/**
	 * @param m_image_m_filtre_netb the m_image_m_filtre_netb to set
	 */
	public void setM_image_m_filtre_netb(MenuItem m_image_m_filtre_netb) {
		this.m_image_m_filtre_netb = m_image_m_filtre_netb;
	}




	/**
	 * @param m_image_m_filtre_sep the m_image_m_filtre_sep to set
	 */
	public void setM_image_m_filtre_sep(MenuItem m_image_m_filtre_sep) {
		this.m_image_m_filtre_sep = m_image_m_filtre_sep;
	}




	/**
	 * @param bp_main the bp_main to set
	 */
	public void setBp_main(BorderPane bp_main) {
		this.bp_main = bp_main;
	}




	/**
	 * @param bp_secondpane the bp_secondpane to set
	 */
	public void setBp_secondpane(BorderPane bp_secondpane) {
		this.bp_secondpane = bp_secondpane;
	}




	/**
	 * @param bp_click the bp_click to set
	 */
	public void setBp_click(BorderPane bp_click) {
		this.bp_click = bp_click;
	}




	/**
	 * @param bp_image the bp_image to set
	 */
	public void setBp_image(BorderPane bp_image) {
		this.bp_image = bp_image;
	}




	/**
	 * @param im the im to set
	 */
	public static void setIm(ImageFrame im) {
		MainFrame.im = im;
	}




	/**
	 * @return the menuItem_image_recadrer
	 */
	public CheckMenuItem getMenuItem_image_recadrer() {
		return menuItem_image_recadrer;
	}




	/**
	 * @return the df
	 */
	public static DrawFrame getDf() {
		return df;
	}




	/**
	 * @return the sr
	 */
	public static SelectRect getSr() {
		return sr;
	}




	/**
	 * @param menuItem_image_recadrer the menuItem_image_recadrer to set
	 */
	public void setMenuItem_image_recadrer(CheckMenuItem menuItem_image_recadrer) {
		this.menuItem_image_recadrer = menuItem_image_recadrer;
	}




	/**
	 * @param df the df to set
	 */
	public static void setDf(DrawFrame df) {
		MainFrame.df = df;
	}




	/**
	 * @param sr the sr to set
	 */
	public static void setSr(SelectRect sr) {
		MainFrame.sr = sr;
	}
	


}
